package org.hxyjs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.hxyjs.dto.DTO;
import org.hxyjs.entity.Label;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.LabelService;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


@CrossOrigin
@RestController //@Controller注解是用spring容器生成 对象 默认对象名 labelController
 //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /labelController
public class LabelController {
	@Resource
	LabelService labelService;
	@GetMapping("/labels")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<Label> getAllLabels() throws Exception{
		System.out.println("查询全部");
		return labelService.getAllLabels();
	}

	@RequestMapping("/getLabelByUno/{uno}")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<Label> getLabelByUno(@PathVariable("uno") String uno) throws Exception{
		System.out.println(uno + "查询成功");
		return labelService.getLabelByUno(uno);
	}
	//@SystemMail(op = "Label:addLabel",queueName = "family.mail",module = "用户管理",methods = "用户注册")
	@PostMapping("/label")
	@ResponseBody
	public DTO<Label> addLabelByUno(@RequestBody Label label) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加成功");
		return labelService.addLabelByUno(label);
	}

	@DeleteMapping("/deleteLabel/{uno}")
	@ResponseBody
	public DTO<Label> deleteLabel(@PathVariable("uno") String uno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println(uno+"删除成功");
		return labelService.deleteLabel(uno);
	}

	@RequestMapping("/getLabelsWithOrders/{uno}")
	@ResponseBody
	public DTO<Label> getLabelsWithOrders(@PathVariable("uno") String uno,HttpServletRequest request,HttpServletResponse response) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询学生选课111");
		DTO<Label> dto = labelService.getLabelsWithOrders(uno);
		saveSelectCourseCookieProcess(dto.getT(),request,response);
		return  dto;
	}
	@PatchMapping(value = "/label",headers = {"content-type=application/json"},consumes = MediaType.APPLICATION_JSON_VALUE
	)//部分修改 //部分修改
	public DTO<Label> updateLabel(@RequestBody Label label) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改成功");
		return labelService.updateLabel(label);
	}
//	@RequestMapping("/labels/pageInfo")
	@GetMapping("/getLabelsByPage")
public DTO<Label> getLabelsByPage(@SpringQueryMap PageInfo pageInfo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询分页");
		return labelService.getLabelsByPage(pageInfo);
	}
	public DTO<Label> saveSelectCourseCookieProcess(Label label, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
		//cookie是以key value形式存放在浏览器的文本
		DTO dto = new DTO(200,"cookie保存成功");
//		if(label!=null&&label.getUno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			Optional<List<OrderForm>> orderFormsOp = Optional.ofNullable(label.getOrderForms());
//			List<OrderForm> orderForms = null;
//			if (orderFormsOp.isPresent()&&orderFormsOp.get().size()>0) {
//				orderForms = new ArrayList<OrderForm>(label.getOrderForms());
//
//				for (OrderForm orderForm : label.getOrderForms()) {
//					Cookie orderFormsCookie = new Cookie("label" + label.getUno() + "_" + orderForm.getUrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderForm), "UTF-8"));
//
//					//labelCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//					orderFormsCookie.setPath("/");
//					orderFormsCookie.setDomain("localhost");
//					orderFormsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//					response.addCookie(orderFormsCookie);
//
//				}
//			}
//			label.setOrderForms(null);
//			Cookie labelCookie = new Cookie("label" + label.getUno(), URLEncoder.encode(objectMapper.writeValueAsString(label), "UTF-8"));
//			labelCookie.setPath("/");
//			labelCookie.setDomain("localhost");
//			labelCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(labelCookie);
//			label.setOrderForms(orderForms);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
		return dto;
	}



	@RequestMapping("/activate/{uno}")
	@ResponseBody
	public DTO<Label> activate(@PathVariable("uno") String uno) throws Exception{

		return labelService.activate(uno);
	}


	@RequestMapping("/lettory/{uno}/{giftName}")
	@ResponseBody
	public DTO<Label> lettory(@PathVariable("uno") String uno,@PathVariable("giftName")String giftName) throws Exception{

		return labelService.lettory(uno,giftName);
	}



}
